package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mapper.OrderInfoMapper;
import com.qs.pojo.entity.OrderInfo;
import com.qs.pojo.entity.OrderItem;
import com.qs.mapper.OrderItemMapper;
import com.qs.service.IOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单详情表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-12-07
 */
@Service
@RequiredArgsConstructor
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {

    private final OrderInfoMapper orderInfoMapper;

    @Override
    public Map<String, Object> getGoodsIds(String orderId, Integer userId) {
        QueryWrapper<OrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        List<OrderItem> orderItems = baseMapper.selectList(wrapper);
        List<Integer> goodsIds = null;
        if(!CollectionUtils.isEmpty(orderItems)){
            goodsIds = orderItems.stream().map(OrderItem::getGoodsId).collect(Collectors.toList());
        }
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("user_id",userId);
        OrderInfo orderInfo = orderInfoMapper.selectOne(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("goodsData",goodsIds);
        map.put("addressId",orderInfo.getAddressId());
        return map;
    }
}
